#include <stdio.h>
#include "log.h"

#define TAG "TEMP_TAB:"
#define LOG_LOCAL_LEVEL 3

typedef struct {
    int temp;
    uint32_t res_val;
} temp_data_t;

#if 1
/*
 * @brief:下面这个是温度和电阻的表，为了便于后续的比较，这里将电阻的值放大10000倍，原始单位是K,注意比较时
 *
 *         如 原始的为{-40,    190.5562f}, {-40, 1905562},
 */
const temp_data_t temp_data_table[] =
{
    {-40,    1905562},
    {-39,    1834132},
    {-38,    1756740},
    {-37,    1676467},
    {-36,    1595647},
    {-35,    1515975},
    {-34,    1438624},
    {-33,    1364361},
    {-32,    1293641},
    {-31,    1226678},
    {-30,    1163519},
    {-29,    1104098},
    {-28,    1048272},
    {-27,    995847},
    {-26,    946608},
    {-25,    900326},
    {-24,    856778},
    {-23,    815747},
    {-22,    777031},
    {-21,    740442},
    {-20,    705811},
    {-19,    672987},
    {-18,    641834},
    {-17,    612233},
    {-16,    584080},
    {-15,    557284},
    {-14,    531766},
    {-13,    507456},
    {-12,    484294},
    {-11,    462224},
    {-10,    441201},
    {-9 ,    421180},
    {-8 ,    402121},
    {-7 ,    383988},
    {-6 ,    366746},
    {-5 ,    350362},
    {-4 ,    334802},
    {-3 ,    320035},
    {-2 ,    306028},
    {-1 ,    292750},
    { 0,     280170},
    { 1,     268255},
    { 2,     256972},
    { 3,     246290},
    { 4,     236176},
    { 5,     226597},
    { 6,     217522},
    { 7,     208916},
    { 8,     200749},
    { 9,     192988},
    {10,    185600},
    {11,    184818},
    {12,    181489},
    {13,    176316},
    {14,    169917},
    {15,    162797},
    {16,    155350},
    {17,    147867},
    {18,    140551},
    {19,    133536},
    {20,    126900},
    {21,    120684},
    {22,    114900},
    {23,    109539},
    {24,    104582},
    {25,    100000},
    {26,    95762},
    {27,    91835},
    {28,    88186},
    {29,    84784},
    {30,    81600},
    {31,    78608},
    {32,    75785},
    {33,    73109},
    {34,    70564},
    {35,    68133},
    {36,    65806},
    {37,    63570},
    {38,    61418},
    {39,    59343},
    {40,    57340},
    {41,    55405},
    {42,    53534},
    {43,    51725},
    {44,    49976},
    {45,    48286},
    {46,    46652},
    {47,    45073},
    {48,    43548},
    {49,    42075},
    {50,    40650},
    {51,    39271},
    {52,    37936},
    {53,    36639},
    {54,    35377},
    {55,    34146},
    {56,    32939},
    {57,    31752},
    {58,    30579},
    {59,    29414},
    {60,    28250},
    {61,    27762},
    {62,    27179},
    {63,    26523},
    {64,    25817},
    {65,    25076},
    {66,    24319},
    {67,    23557},
    {68,    22803},
    {69,    22065},
    {70,    21350},
    {71,    20661},
    {72,    20004},
    {73,    19378},
    {74,    18785},
    {75,    18225},
    {76,    17696},
    {77,    17197},
    {78,    16727},
    {79,    16282},
    {80,    15860},
    {81,    15458},
    {82,    15075},
    {83,    14707},
    {84,    14352},
    {85,    14006},
    {86,    13669},
    {87,    13337},
    {88,    13009},
    {89,    12684},
    {90,    12360},
    {91,    12037},
    {92,    11714},
    {93,    11390},
    {94,    11067},
    {95,    10744},
    {96,    10422},
    {97,    10104},
    {98,    9789},
    {99,    9481},
    {100,   9180},
    {101,   8889},
    {102,   8610},
    {103,   8346},
    {104,   8099},
    {105,   7870},
    {106,   7665},
    {107,   7485},
    {108,   7334},
};
#else
/*
 * @brief:下面这个是温度和电阻的表，为了便于后续的比较，这里将电阻
 *
 */
const temp_data_t temp_data_table[] =
{
    {-40,    190.5562f},
    {-39,    183.4132f},
    {-38,    175.6740f},
    {-37,    167.6467f},
    {-36,    159.5647f},
    {-35,    151.5975f},
    {-34,    143.8624f},
    {-33,    136.4361f},
    {-32,    129.3641f},
    {-31,    122.6678f},
    {-30,    116.3519f},
    {-29,    110.4098f},
    {-28,    104.8272f},
    {-27,    99.5847f},
    {-26,    94.6608f},
    {-25,    90.0326f},
    {-24,    85.6778f},
    {-23,    81.5747f},
    {-22,    77.7031f},
    {-21,    74.0442f},
    {-20,    70.5811f},
    {-19,    67.2987f},
    {-18,    64.1834f},
    {-17,    61.2233f},
    {-16,    58.4080f},
    {-15,    55.7284f},
    {-14,    53.1766f},
    {-13,    50.7456f},
    {-12,    48.4294f},
    {-11,    46.2224f},
    {-10,    44.1201f},
    {-9 ,    42.1180f},
    {-8 ,    40.2121f},
    {-7 ,    38.3988f},
    {-6 ,    36.6746f},
    {-5 ,    35.0362f},
    {-4 ,    33.4802f},
    {-3 ,    32.0035f},
    {-2 ,    30.6028f},
    {-1 ,    29.2750f},
    { 0,     28.0170f},
    { 1,     26.8255f},
    { 2,     25.6972f},
    { 3,     24.6290f},
    { 4,     23.6176f},
    { 5,     22.6597f},
    { 6,     21.7522f},
    { 7,     20.8916f},
    { 8,     20.0749f},
    { 9,     19.2988f},
    {10,    18.5600f},
    {11,    18.4818f},
    {12,    18.1489f},
    {13,    17.6316f},
    {14,    16.9917f},
    {15,    16.2797f},
    {16,    15.5350f},
    {17,    14.7867f},
    {18,    14.0551f},
    {19,    13.3536f},
    {20,    12.6900f},
    {21,    12.0684f},
    {22,    11.4900f},
    {23,    10.9539f},
    {24,    10.4582f},
    {25,    10.0000f},
    {26,    9.5762f},
    {27,    9.1835f},
    {28,    8.8186f},
    {29,    8.4784f},
    {30,    8.1600f},
    {31,    7.8608f},
    {32,    7.5785f},
    {33,    7.3109f},
    {34,    7.0564f},
    {35,    6.8133f},
    {36,    6.5806f},
    {37,    6.3570f},
    {38,    6.1418f},
    {39,    5.9343f},
    {40,    5.7340f},
    {41,    5.5405f},
    {42,    5.3534f},
    {43,    5.1725f},
    {44,    4.9976f},
    {45,    4.8286f},
    {46,    4.6652f},
    {47,    4.5073f},
    {48,    4.3548f},
    {49,    4.2075f},
    {50,    4.0650f},
    {51,    3.9271f},
    {52,    3.7936f},
    {53,    3.6639f},
    {54,    3.5377f},
    {55,    3.4146f},
    {56,    3.2939f},
    {57,    3.1752f},
    {58,    3.0579f},
    {59,    2.9414f},
    {60,    2.8250f},
    {61,    2.7762f},
    {62,    2.7179f},
    {63,    2.6523f},
    {64,    2.5817f},
    {65,    2.5076f},
    {66,    2.4319f},
    {67,    2.3557f},
    {68,    2.2803f},
    {69,    2.2065f},
    {70,    2.1350f},
    {71,    2.0661f},
    {72,    2.0004f},
    {73,    1.9378f},
    {74,    1.8785f},
    {75,    1.8225f},
    {76,    1.7696f},
    {77,    1.7197f},
    {78,    1.6727f},
    {79,    1.6282f},
    {80,    1.5860f},
    {81,    1.5458f},
    {82,    1.5075f},
    {83,    1.4707f},
    {84,    1.4352f},
    {85,    1.4006f},
    {86,    1.3669f},
    {87,    1.3337f},
    {88,    1.3009f},
    {89,    1.2684f},
    {90,    1.2360f},
    {91,    1.2037f},
    {92,    1.1714f},
    {93,    1.1390f},
    {94,    1.1067f},
    {95,    1.0744f},
    {96,    1.0422f},
    {97,    1.0104f},
    {98,    0.9789f},
    {99,    0.9481f},
    {100,   0.9180f},
    {101,   0.8889f},
    {102,   0.8610f},
    {103,   0.8346f},
    {104,   0.8099f},
    {105,   0.7870f},
    {106,   0.7665f},
    {107,   0.7485f},
    {108,   0.7334f},
};
#endif


/**
 * key:         key value you want to searchpath
 * data_set:    dataset.
 * data_count:  size of dataset.
 */
int temp_search(uint32_t key)
{
    unsigned short pre_index = 0,next_index = 0;

    int l = 0, r = sizeof(temp_data_table)/ sizeof(temp_data_t),mid = 0;
    //unsigned int _key = (unsigned int)(key * MULTIPLE);
	LOGD("key:%d,count:%d",key,r);
    while (l <= r) {
        mid = (int)((l + r)/2.0f + 1); //避免溢出
        //LOGI("mid:%d",mid);
        if((temp_data_table[mid + 1].res_val <= key) && (temp_data_table[mid - 1].res_val >= key)) {
            pre_index = mid - 1;
            next_index = mid + 1;
            LOGD("temp[%d]:%d°C\n",mid,temp_data_table[mid].temp); //打印要查找元素的位置
            break;
        }
        //if(key > (temp_data_table[mid].res_val * MULTIPLE))
        if(key > (temp_data_table[mid].res_val))//往前查找
            r= mid;
        else
            l = mid - 1;
        if ((r - l) <= 3)
            //如果不确定，可以在这里进行精细化比较
            break;
    }
    //if (mid > r)
    //    mid = r;
    return temp_data_table[mid].temp;
}

